# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import List

import paddle

from paddle3d.datasets.metrics import MetricABC
from paddle3d.sample import Sample
from paddle3d.utils.logger import logger

__all__ = ["AccuracyMetric"]


class AccuracyMetric(MetricABC):
    def __init__(self, num_classes: int):
        # classes
        self.num_classes = num_classes
        self.predictions = []
        self.ground_truths = []

    def update(self,
               predictions: List[Sample],
               ground_truths: List[Sample] = None):
        self.predictions.append(predictions)
        self.ground_truths.append(ground_truths)

    def compute(self, verbose=False):
        self.predictions = paddle.concat(self.predictions, axis=0)
        self.ground_truths = paddle.concat(self.ground_truths, axis=0)
        accuracy = paddle.metric.accuracy(self.predictions, self.ground_truths)
        if verbose:
            logger.info("Acc {:.3f}".format(float(accuracy)))
        return dict(accuracy=accuracy)
